package com.atguigu.lease.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
//扫描mapper接口所在的包
@MapperScan(basePackages = "com.atguigu.lease.web.*.mapper")
public class StgyMpConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //创建mybatis的 mp拦截器
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //创建mp内部的自己的 分页拦截器
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);//设置要操作的数据库类型 默认mysql
        //将分页拦截器注册给 MybatisPlusInterceptor
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        //返回MybatisPlusInterceptor注入到容器中
        return interceptor;
    }
    // 向容器中注入objectMapper对象时，如果存在同名的对象 ioc容器会报错
    //对象属性类型转换器： jackson将对象转为json字符串时，每个属性值应该如何转换
    @Bean
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        // 全局配置序列化返回 JSON 处理
        SimpleModule simpleModule = new SimpleModule();
        // 将使用String来序列化Long类型
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        // 将使用String来序列化long类型
//        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }

}
